package com.cyw.controller;


import cn.hutool.json.JSONUtil;
import com.cyw.pojo.Book;
import com.cyw.service.IBookService;
import com.cyw.utils.RedisUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author cyw
 * @since 2022-10-30
 */
@RestController
@RequestMapping("/book")
@Api(tags = "图书管理")
public class BookController {

    @Autowired
    private IBookService service;
    @Resource
    private RedisUtil redisUtil;

    @GetMapping("/findAll")
    @ApiOperation(value = "查找所有图书接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization",//参数名字
                    value = "token",//参数的描述
                    required = true,//是否必须传入
                    //paramType定义参数传递类型：有path,query,body,form,header
                    paramType = "header"
            )
    })
    @ResponseBody
    public Map<String,Object> findAllBook() {
        Map<String,Object> res = new HashMap<>();
        res.put("errorCode", 0);
        String booksString = redisUtil.get("books");
        List<Book> books = null;
        if (booksString == null) {
            books = service.list();
            String objectJson = JSONUtil.toJsonStr(books);
            redisUtil.set("books", objectJson);
            redisUtil.setExpire("books", 3600);
        } else {
            books = JSONUtil.toList(JSONUtil.parseArray(booksString),Book.class);
        }
        res.put("books", books);
        return res;
    }

    @GetMapping("/findOne/{id}")
    @ApiOperation(value = "根据id查找图书接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",//参数名字
                    value = "id",//参数的描述
                    required = true,//是否必须传入
                    paramType = "path"//paramType定义参数传递类型：有path,query,body,form,header
            ),
            @ApiImplicitParam(name = "Authorization",//参数名字
                    value = "token",//参数的描述
                    required = true,//是否必须传入
                    //paramType定义参数传递类型：有path,query,body,form,header
                    paramType = "header"
            )
    })
    @ResponseBody
    public Map<String,Object> findOneBook(@PathVariable("id") Integer id) {
        Map<String,Object> res = new HashMap<>();
        res.put("errorCode", 0);
        Book book = service.getById(id);
        res.put("book", book);
        return res;
    }

    @PostMapping("/add")
    @ApiOperation(value = "新增图书接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization",//参数名字
                    value = "token",//参数的描述
                    required = true,//是否必须传入
                    //paramType定义参数传递类型：有path,query,body,form,header
                    paramType = "header"
            )
    })
    @ResponseBody
    public Map<String,Object> addBook(@RequestBody Book book) {
        Map<String,Object> res = new HashMap<>();
        if (service.save(book)) {
            res.put("errorCode", 0);
            res.put("msg", "添加成功!");
        } else {
            res.put("errorCode", -1);
            res.put("msg", "添加失败!");
        }
        return res;
    }

    @DeleteMapping("/deleteOne/{id}")
    @ApiOperation(value = "根据id删除图书接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",//参数名字
                    value = "id",//参数的描述
                    required = true,//是否必须传入
                    paramType = "path"//paramType定义参数传递类型：有path,query,body,form,header
            ),
            @ApiImplicitParam(name = "Authorization",//参数名字
                    value = "token",//参数的描述
                    required = true,//是否必须传入
                    //paramType定义参数传递类型：有path,query,body,form,header
                    paramType = "header"
            )
    })
    @ResponseBody
    public Map<String,Object> deleteBookById(@PathVariable("id") Integer id) {
        Map<String,Object> res = new HashMap<>();
        if (service.removeById(id)) {
            res.put("errorCode", 0);
            res.put("msg", "删除成功!");
        } else {
            res.put("errorCode", -1);
            res.put("msg", "删除失败!");
        }
        return res;
    }

    @PutMapping("/updateOne")
    @ApiOperation(value = "更新图书接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization",//参数名字
                    value = "token",//参数的描述
                    required = true,//是否必须传入
                    //paramType定义参数传递类型：有path,query,body,form,header
                    paramType = "header"
            )
    })
    @ResponseBody
    public Map<String,Object> updateBook(@RequestBody Book book) {
        Map<String,Object> res = new HashMap<>();
        if (service.updateById(book)) {
            res.put("errorCode", 0);
            res.put("msg", "修改成功!");
        } else {
            res.put("errorCode", -1);
            res.put("msg", "修改失败!");
        }
        return res;
    }
}

